<?php
include_once("common/init.php");

if($_POST['goodsid']){$goodsid = !empty($_POST['goodsid']) ? intval($_POST['goodsid']) : '0';}else{$goodsid = !empty($_REQUEST['goodsid']) ? intval($_REQUEST['goodsid']) : '0';}
$sums = !empty($_POST['sums']) ? intval($_POST['sums']) : '1';
$info = db_get_row("select * from goods where id=".$goodsid);
if($info['amount']<$sums){
	goBakMsg("该商品数量不够，请重新选择!");
	exit;
}

$rowu = db_get_row("select * from goods where userid='". $_SESSION["id"] ."' and id=". $goodsid ."");
if ($rowu["id"]) {
	goBakMsg("自己的，不用购买！！");
	die;
}

$row = db_get_row("select * from cart where goodsid=". $goodsid." and userid=".$_SESSION["id"]);
if ($row["id"]) {
	goBakMsg("此商品已在购物车里");
	die;
}
$data = array();
$data["goodsid"] = "'". $goodsid ."'";
$data["userid"] = "'". $_SESSION["id"] ."'";
$data["sums"] = "'". $sums ."'";
$data["usersid"] = "'". $info["userid"] ."'";
db_add("cart", $data);
header("location:cart.php");
// goBakMsg("已添加至购物车");
?>